<?php
namespace backend\controllers;

use Yii;
use common\models\LoginForm;
use bc\controller\BackendController;

/**
 * Site controller
 */
class SiteController extends BackendController
{

    public function actionIndex()
    {
    	
    	$default_start_time_str = strtotime('-7 day');
    	$default_start_time = date('Y-m-d', $default_start_time_str);
    	
    	$default_end_time_str = strtotime('-1 day');
    	$default_end_time = date('Y-m-d', $default_end_time_str);
    	
    	$start_time = $this->get('start_time', '');
    	$end_time = $this->get('end_time', '');
    	
    	
    	$start_time = $start_time ? $start_time : $default_start_time;
    	$end_time = $end_time ? $end_time : $default_end_time;
    	
    	$dates = $this->showDates($start_time, $end_time);
    	$view['dates'] = json_encode($dates);
    	
    	$model = new \bc\model\unsubscribe\LogModel();
    	$data = $model->getStatistics($start_time, $end_time);
    	
    	$json_data = $this->arrangeDate($dates, $data);
    	$view['json_data'] = $json_data;
    	
    	
    	$view['start_time'] = $start_time;
    	$view['end_time'] = $end_time;
    	
    	return $this->render('index', $view);
    }
	
    
    public function actionAjaxusers()
    {
    	$date_time = $this->post('date_time');
    	$time = strtotime($date_time);
    	$li = '';
    	
    	if($time > 0)
    	{
    		$model = new \bc\model\unsubscribe\LogModel();
    		$list = $model->getListJoinUser(['unsubscribe_time' => $time]);
    		if($list)
    		{
    			foreach ($list as $row)
    			{
    				$li .= '<li>' . $row['nickname'] . '</li>';
    			}
    		}
    	}
    	
    	echo $li;
    }
  
    
    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }
	
    /**
     * 测试主页
     * @author pwstrick
     */
    public function actionHome() {
    	
    	$this->redirect('/product/list', 301);
    	
    	$this->layout = 'default';
    	$this->getView()->title = '主页';
    	$this->getView()->params['break'] = array();
    	return $this->render('home');
    	$this->getView()->title = '主页';
    	$this->getView()->params['breadcrumbs'] = array(
    		array('url'=>'#', 'name'=>'用户列表'),
    		array('url'=>'#', 'name'=>'用户添加', 'class'=>'current')
    	);
    	$this->getView()->params['js'] = 'list';
    	
    	return $this->render('home');
    }
    
    /**
     * 测试表单
     * @author pwstrick
     */
    public function actionForm() {
    	$this->layout = 'default';
    	$this->getView()->title = '表单提交';
    	$this->getView()->params['breadcrumbs'] = array(
    			array('url'=>'#', 'name'=>'表单'),
    			array('url'=>'#', 'name'=>'表单添加', 'class'=>'current')
    	);
    	$this->getView()->params['js'] = 'form';
    	
    	return $this->render('form');
    }
    
    /**
     * 测试弹层
     * @author pwstrick
     */
    public function actionLayer() {
    	$this->layout = 'layer';
    	$this->getView()->title = '弹出层';
    	
    	return $this->render('layer');
    }
    
    
    public function actionTest()
    {
    	print_r(Yii::$app->params);exit;
    	return $this->render('test');
    }
    
    /**
     * 邮件发送DEMO
     */
    public function actionMail()
    {
    	$this->nolayout();
    	$mail= Yii::$app->mailer->compose();
    	$mail->setTo('2657296@qq.com');
    	$mail->setSubject("邮件测试");
    	$mail->setTextBody('你好，世界 ');
    	if($mail->send())
    		echo "success";
    	else
    		echo "failse";
    	die();
    }
    
    /**
     * 
     * @param string $start
     * @param string $end
     */
    protected function showDates($start, $end)
    {
    	$list = [];
    	while ($end >= $start)
    	{
    		$list[] = $end;
    		$end = strtotime('-1 day', strtotime($end));
    		$end = date('Y-m-d', $end);
    		
    	}
    	$list = array_reverse($list);
    	return $list;
    }
    
    /**
     * 按照一定格式整理数据
     * @param array $date
     * @param json $data
     */
    protected function arrangeDate($date, $data)
    {
    	$list = [];
    	foreach ($date as $row)
    	{
    		$tmp['date'] = $row;
    		$tmp['total'] = 0;
    		foreach ($data as $item)
    		{
    			if($row == $item['date'])
    			{
    				$tmp['total'] = $item['total'];
    			}
    		}
    		$list[] = $tmp['total'];
    	}
    	
    	return json_encode($list);
    }
}
